﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.Design
Imports System.Globalization
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.Activities
    Friend Class StateDesignerConnectorLayoutSerializer
        Inherits ConnectorLayoutSerializer
        ' Methods
        Protected Overrides Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (type Is Nothing) Then
                Throw New ArgumentNullException("type")
            End If
            Dim connector As StateDesignerConnector = Nothing
            Dim service As IReferenceService = [TryCast](serializationManager.GetService(GetType(IReferenceService)),IReferenceService)
            Dim designer As FreeformActivityDesigner = [TryCast](serializationManager.Context.Item(GetType(FreeformActivityDesigner)),FreeformActivityDesigner)
            If ((Not designer Is Nothing) AndAlso (Not service Is Nothing)) Then
                Dim source As DesignerLayoutConnectionPoint = Nothing
                Dim target As ConnectionPoint = Nothing
                Dim transitionInfo As TransitionInfo = Nothing
                Dim rootStateDesigner As StateDesigner = Nothing
                Try 
                    Dim connectorConstructionArguments As Dictionary([Of] String, String) = MyBase.GetConnectorConstructionArguments(serializationManager, type)
                    If ((connectorConstructionArguments.ContainsKey("EventHandlerName") AndAlso connectorConstructionArguments.ContainsKey("SetStateName")) AndAlso connectorConstructionArguments.ContainsKey("TargetStateName")) Then
                        Dim reference As CompositeActivity = DirectCast(service.GetReference(connectorConstructionArguments.Item("EventHandlerName")), CompositeActivity)
                        Dim setState As SetStateActivity = DirectCast(service.GetReference(connectorConstructionArguments.Item("SetStateName")), SetStateActivity)
                        Dim activity3 As StateActivity = DirectCast(service.GetReference(connectorConstructionArguments.Item("TargetStateName")), StateActivity)
                        transitionInfo = New TransitionInfo(setState, reference) { _
                            .TargetState = activity3 _
                        }
                    End If
                    If ((connectorConstructionArguments.ContainsKey("SourceActivity") AndAlso connectorConstructionArguments.ContainsKey("SourceConnectionIndex")) AndAlso (connectorConstructionArguments.ContainsKey("SourceConnectionEdge") AndAlso connectorConstructionArguments.ContainsKey("EventHandlerName"))) Then
                        Dim associatedDesigner As StateDesigner = DirectCast(StateDesigner.GetDesigner([TryCast](service.GetReference(connectorConstructionArguments.Item("SourceActivity")),Activity)), StateDesigner)
                        Dim eventHandler As CompositeActivity = DirectCast(service.GetReference(connectorConstructionArguments.Item("EventHandlerName")), CompositeActivity)
                        rootStateDesigner = associatedDesigner.RootStateDesigner
                        Dim designerEdges As DesignerEdges = DirectCast(Enum.Parse(GetType(DesignerEdges), connectorConstructionArguments.Item("SourceConnectionEdge")), DesignerEdges)
                        Dim connectionIndex As Integer = Convert.ToInt32(connectorConstructionArguments.Item("SourceConnectionIndex"), CultureInfo.InvariantCulture)
                        If (((Not associatedDesigner Is Nothing) AndAlso (Not eventHandler Is Nothing)) AndAlso ((designerEdges <> DesignerEdges.None) AndAlso (connectionIndex >= 0))) Then
                            source = New DesignerLayoutConnectionPoint(associatedDesigner, connectionIndex, eventHandler, designerEdges)
                        End If
                    End If
                    If ((connectorConstructionArguments.ContainsKey("TargetActivity") AndAlso connectorConstructionArguments.ContainsKey("TargetConnectionIndex")) AndAlso connectorConstructionArguments.ContainsKey("TargetConnectionEdge")) Then
                        Dim designer4 As ActivityDesigner = StateDesigner.GetDesigner([TryCast](service.GetReference(connectorConstructionArguments.Item("TargetActivity")),Activity))
                        Dim designerEdge As DesignerEdges = DirectCast(Enum.Parse(GetType(DesignerEdges), connectorConstructionArguments.Item("TargetConnectionEdge")), DesignerEdges)
                        Dim num2 As Integer = Convert.ToInt32(connectorConstructionArguments.Item("TargetConnectionIndex"), CultureInfo.InvariantCulture)
                        If (((Not designer4 Is Nothing) AndAlso (designerEdge <> DesignerEdges.None)) AndAlso (num2 >= 0)) Then
                            target = New ConnectionPoint(designer4, designerEdge, num2)
                        End If
                    End If
                Catch obj1 As Object
                End Try
                If (((transitionInfo Is Nothing) OrElse (source Is Nothing)) OrElse (target Is Nothing)) Then
                    Return connector
                End If
                connector = rootStateDesigner.FindConnector(transitionInfo)
                If (connector Is Nothing) Then
                    rootStateDesigner.AddingSetState = False
                    Try 
                        Return [TryCast](designer.AddConnector(source, target),StateDesignerConnector)
                    Finally
                        rootStateDesigner.AddingSetState = True
                    End Try
                End If
                connector.Source = source
                connector.Target = target
                connector.ClearConnectorSegments
            End If
            Return connector
        End Function

        Protected Overrides Function GetProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As PropertyInfo()
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim list As New List([Of] PropertyInfo)(MyBase.GetProperties(serializationManager, obj)) { GetType(StateDesignerConnector).GetProperty("SetStateName", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(StateDesignerConnector).GetProperty("SourceStateName", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(StateDesignerConnector).GetProperty("TargetStateName", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(StateDesignerConnector).GetProperty("EventHandlerName", (BindingFlags.NonPublic Or BindingFlags.Instance)) }
            Return list.ToArray
        End Function

    End Class
End Namespace

